/*
 * This servlet handles the manage teachers part of the website
 */
package controllers.managecontrollers;

import classes.Breadcrumbs;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Employee;
import models.Role;
import models.User;
import dao.EmployeeDao;
import classes.HibernateUtil;
import dao.RoleDao;
import dao.UserDao;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author Achie
 */
public class ManageTeacherController extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //----------------------------------------------------------------
        //Load roles and redirect
        //----------------------------------------------------------------
        HibernateUtil hu = new HibernateUtil();
        RoleDao rd = new RoleDao(hu.openSession());
        List<Role> roleList = rd.findAll();
        request.setAttribute("roleList", roleList);
        
        hu.close(); //close connection
        
        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Teacher", "Manage teachers"),
            new Breadcrumbs("Teacher/edit?id", "Add teacher")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);
        
        //redirect user to the add edit teacher page
        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/add_edit_teachers.jsp");
        dispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HibernateUtil hu = new HibernateUtil();
        //----------------------------------------------------------------
        //Get inputs from form
        //----------------------------------------------------------------
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String confirmpassword = request.getParameter("confirmpassword");
        String firstname = request.getParameter("firstname");
        String insertion = request.getParameter("insertion");
        String lastname = request.getParameter("lastname");
        String description = request.getParameter("description");
        long roleid = Long.parseLong(request.getParameter("role")); 

        if (request.getParameter("formoption").equals("teacheredit")) { //wijzig
            //Check of alles ingevuld is

            long id = Long.parseLong(request.getParameter("teacherid"));

            EmployeeDao ed = new EmployeeDao(hu.openSession());
            UserDao ud = new UserDao(hu.openSession());

            Employee emptemp = ed.get(id);
            User ustemp = emptemp.getUser();
            ustemp.setEmail(email); //save user object
            ud.update(ustemp);

            emptemp.setFirstname(firstname); //save employee object
            emptemp.setInsertion(insertion);
            emptemp.setLastname(lastname);
            emptemp.setRole(makeRole(roleid));
            emptemp.setDescription(description);
            ed.update(emptemp);

        } else { //voeg toe

            //----------------------------------------------------------------
            //Check inputs (MOET NOG VERBETERD WORDEN)
            //----------------------------------------------------------------

            if (email.trim().isEmpty() || password.trim().isEmpty() || confirmpassword.trim().isEmpty()) {
                response.sendRedirect("jsp/add_edit_teachers.jsp");
            }
            if (!password.equals(confirmpassword)) {
                response.sendRedirect("jsp/add_edit_teachers.jsp");
            }

            //----------------------------------------------------------------
            //Get teacher role
            //----------------------------------------------------------------
            Role role = makeRole(roleid);

            //----------------------------------------------------------------
            //Create user and employee object and save them
            //----------------------------------------------------------------
            long member_id = System.nanoTime();
            User user = new User(member_id, email, password, firstname, insertion, lastname);
            user.setDate(); //set join date
            UserDao ud = new UserDao(hu.openSession());
            ud.save(user);

            Employee employee_teacher = new Employee(member_id, user, role, firstname, lastname, insertion, description);
            EmployeeDao ed = new EmployeeDao(hu.openSession());
            ed.save(employee_teacher);
        }
        hu.close(); //close connection
        response.sendRedirect("Teacher");
    }

    public Role makeRole(long roleid) { //makes the role by the id
        HibernateUtil hu = new HibernateUtil();
        RoleDao rd = new RoleDao(hu.openSession());
        return rd.get(roleid);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
